<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container" style="margin-bottom:20px">
      <span style="margin-right:10px">AppName</span>
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;margin-left: 10px" class="filter-item"
                placeholder="AppName" v-model="listQuery.appname" clearable>
      </el-input>
      <span style="margin-right:10px">名称</span>
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;margin-left: 10px" class="filter-item"
                placeholder="名称" v-model="listQuery.title" clearable>
      </el-input>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search"
                 @click="handleFilter">搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" @click="executorCreate"
                 :type="pageBoolean.addColor" icon="el-icon-plus">{{ pageBoolean.addText }}
      </el-button>
    </div>

    <el-table
      ref='list'
      :key='tableKey'
      :data="list"
      v-loading="listLoading"
      element-loading-text="数据加载中，请稍后"
      highlight-current-row
      fit
      select-on-indeterminate
      style="width: 100%">

      <!-- <el-table-column align="center" label="ID" min-width="30">
        <template slot-scope="scope">
          <span class="link-type">{{scope.row.id}}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="序号"
        type="index"
        width="50">
      </el-table-column>
      <el-table-column align="center" label="AppName" min-width="100">
        <template slot-scope="scope">
          <span class="link-type">{{scope.row.appname}}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="100" align="center" label="名称" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.title}}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column align="center" label="排序" min-width="40">
        <template slot-scope="scope">
          <span class="link-type">{{scope.row.order}}</span>
        </template>
      </el-table-column> -->

      <el-table-column min-width="60" align="center" label="注册方式" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-html="formatRegDesc(scope.row.addressType)"></span>
        </template>
      </el-table-column>

      <el-table-column min-width="120" align="center" label="OnLine机器地址" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-html="formatAddressList(scope.row.addressList)"></span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" min-width="80">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="executorModify(scope.row)">编辑
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="warnBeforeDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.currentPage"
                     :page-sizes="[10,20,30,50]" :page-size="listQuery.length"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="dialogStatus=='create' ? '新增执行器' : '编辑执行器'" :visible.sync="dialogFormVisible" center>
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="AppName" label-width="80px" prop="appname">
          <el-input v-model="form.appname" auto-complete="off" maxlength="180"
                    placeholder="请输入AppName"></el-input>
        </el-form-item>
        <el-form-item label="名称" label-width="80px" prop="title">
          <el-input v-model="form.title" auto-complete="off" maxlength="180"
                    placeholder="请输入名称"></el-input>
        </el-form-item>
        <!-- <el-form-item label="排序" label-width="80px" prop="order">
          <el-input-number v-model="form.order" :min="1" placeholder="请输入排列顺序"></el-input-number>
        </el-form-item> -->

        <el-form-item label="注册方式" prop="addressType">
          <el-select v-model="form.addressType" placeholder="请选择">
            <el-option v-for="item of addressTypeEnum"
                       :key="item.code"
                       :label="item.msg"
                       :value="item.code"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="地址列表" label-width="80px" prop="addressList">
          <el-input v-model="form.addressList" auto-complete="off" maxlength="180" type="textarea"
                    placeholder="请输入执行器地址，多个地址用逗号分隔"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="addForm">确 定</el-button>
        <el-button v-else type="primary" @click="updateForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {create, getGroupList, remove, update} from "../../api/group";
  import qs from 'qs';

  export default {
    name: 'list',
    props: {},
    components: {},
    data() {
      return {
        addressTypeEnum: [{code: 0, msg: '自动注册'}, {code: 1, msg: "手动注册"}],
        dialogStatus: 'create',
        dialogFormVisible: false,
        userTypeCode: null,
        userTypes: [{code: 0, msg: '超级管理员'}, {code: 1, msg: '普通用户'}],

        modifyData: '',
        form: {
          id: null, appname: '', title: '', addressType: null, addressList: ''
        },
        rules: {
          appname: [{required: true, message: '请输入AppName', trigger: 'blur'}],
          title: [{required: true, message: '请输入执行器名称', trigger: 'blur'}],
          // order: [{required: true, message: '请输入排列顺序', trigger: 'blur'}],
          addressType: [{required: true, message: '请选择注册类型', trigger: 'blur'}]
        },
        list: null,
        total: 0,
        listLoading: true,
        listQuery: {
          appname: '',
          title: '',
          start: 0,
          length: 10,
          currentPage: 1
        },
        tableKey: 0,
        pageBoolean: {
          delColor: 'primary',
          delText: '批量删除',
          addColor: 'success',
          addText: '添加执行器'
        },
      }
    },
    created() {
    },
    mounted() {
      this.fetchTableData()
    },
    watch: {},
    methods: {
      fetchTableData() {
        getGroupList(qs.stringify(this.listQuery)).then(res => {
          this.listLoading = false
          const {data, recordsTotal} = res
          this.list = data
          this.total = recordsTotal
          if (this.total === 0) {
            this.$message({
              message: '执行器列表为空',
              type: 'warning'
            })
          }
        }).catch(function (err) {
          console.log(err)
        })
      },
      executorCreate() {
        this.form = {
          id: null, appname: '', title: '', addressType: null, addressList: ''
        }
        this.dialogFormVisible = true
        this.dialogStatus = 'create'
      },
      addForm() {
        // console.info("addForm",this.form)
        this.$refs['form'].validate((valid) => {
          if (valid) {
            //去除表单中id
            const param = qs.stringify({
              'appname': this.form.appname,
              'title': this.form.title,
              'addressType': this.form.addressType,
              'addressList': this.form.addressList
            });
            create(param).then(res => {
              if (res.code === 200) {
                this.dialogFormVisible = false
                this.$message({
                  message: "新增成功",
                  type: 'success'
                })
                this.handleRefresh()
              } else {
                this.$message(res.msg)
              }
              this.listLoading = false
            }).catch(function (err) {
              console.log(err.message)
            })
          } else {
            return false;
          }
        });
      },
      executorModify(row) { // 修改执行器信息
        this.dialogFormVisible = true
        this.dialogStatus = 'update'
        this.form = row
      },
      updateForm() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            update(this.form).then(res => {
              if (res.code === 200) {
                this.dialogFormVisible = false
                this.$message({
                  message: "更新成功",
                  type: 'success'
                })
              } else {
                this.$message(res.msg)
              }
            }).catch(function (err) {
              console.log(err.message)
            })
          } else {
            return false;
          }
        });
      },
      /*modifyComplete(data) {
        if (!data) return false
        this.handleRefresh()
        this.dialogFormVisible = false
      },*/
      warnBeforeDelete(idx, row) {
        this.$confirm(
          '确认删除当前执行器？',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          }
        ).then(response => {
          this.batchDelete(idx, row)
        }).catch(reject => console.log(reject))
      },
      batchDelete(idx, row) {
        remove(row.id).then(res => {
            if (res.code === 200) {
              this.$message({
                message: '删除成功',
                type: 'success',
                duration: 2000
              })
              const index = this.list.indexOf(idx)
              this.list.splice(idx, 1)
              this.total--
            } else {
              this.$message({
                message: '删除失败',
                type: 'error',
                duration: 2000
              })
            }
          }
        ).catch(function (err) {
          console.log(err.message)
        })
      },

      formatAddressList(addressList) {
        if (addressList !== null && addressList !== '') {
          let addressArr = addressList.split(",")
          let showAddress = ''
          addressArr.forEach(function (val) {
            showAddress += val + '<br>'
          })
          return showAddress
        }else{
          return "无"
        }
      },
      formatRegDesc(type){
          switch(type){
            case 0:
              return "自动注册"
            case 1:
              return ""
          }
      },

      handleFilter() {
        // console.info("handlerFilter")
        this.fetchTableData()
      },
      //每页数改变回调
      handleSizeChange(val) {
        this.listQuery.length = val
        this.fetchTableData()
      },
      //翻页回调
      handleCurrentChange(val) {
        this.listQuery.start = (val - 1) * this.listQuery.length
        this.fetchTableData()
      },
      handleRefresh() {
        this.list = null
        this.total = null
        this.fetchTableData()
      }
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss" slot-scope>

  .app-container {
    padding: 20px;
  }

  .pagination-container {
    margin-top: 30px;
  }
</style>
<style rel="stylesheet/scss" lang="scss">
  .dialogModifyLessonManage {
    .el-dialog__header {
      padding-bottom: 0;
    }
    .el-dialog__body {
      padding: 0
    }
    .createPost-main-container {
      padding: 20px 0 !important;
    }
  }
</style>
